<?php
/*----------------------------------------------------------------------------------------------
Kit Applicatif PHP
Noyau générique d'applications PHP / generic kernel for PHP applications
noyau 0.4 / PHP 4
http://code.fredbird.org/kap

gestion de comptes utilisateurs, stockage sql


file version 2004-12-02 12:06:14
----------------------------------------------------------------------------------------------*/

class kap_usermanager extends kap_core_component {
/*----------------------------------------------------------------------------------------------
                                                                           parametres par defaut
----------------------------------------------------------------------------------------------*/
	var $default_parameters=array(
		'sqlTable'=> 'kap_users', // nom de la table SQL
		'idField'=>'id', // nom de la clef primaire
		'autoId'=>true,
		'loginField'=>'login',// nom du champ login
		'pwdField'=>'pwd',// nom du champ mot de passe
		'statusField'=>'user_status',// nom du champ statut
		'dateCreationField'=>'date_creation',// nom du champ date de creation
		'screenField'=>'screen_name', // nom du champ d'affichage de l'identité
		'pwdEncryption'=>'md5' // none|md5 encryption du mot de passe
	);
/*----------------------------------------------------------------------------------------------
                                                                           constructeur eventuel
----------------------------------------------------------------------------------------------*/
	/*function kap_(& $controller) {
		// appel du constructeur parent
		$const=get_parent_class($this);
		parent::$const($controller);
		// suite
	}*/
/*----------------------------------------------------------------------------------------------
                                                                              methodes publiques
a lier dans components/controller.kap-core.php et api.kap-core.php
----------------------------------------------------------------------------------------------*/
	function getIdByLoginPwd($login,$pwd) {
		kapSqlOpen();
		// recherche du login
		$login=kapSqlSecureValue($login);
		$sql="SELECT {$this->parameters['idField']} AS id, {$this->parameters['pwdField']} AS pwd"
		." FROM {$this->parameters['sqlTable']}"
		." WHERE {$this->parameters['loginField']}=".$login." LIMIT 1";
		if (!$result=kapSqlUniqueResult($sql)) return false;
		// comparaison du mot de passe
		$pwd=$this->crypt($pwd);
		//echo "sent : $pwd / ".$result['pwd'];
		if ($pwd!=$result['pwd']) return false;
		// renvoi de l'id
		return $result['id'];
	}
	
	function getById($id) {
		kapSqlOpen();
		$id=kapSqlSecureValue($id);
		$sql="SELECT *, {$this->parameters['idField']} AS id"
		.", {$this->parameters['loginField']} AS login"
		.", {$this->parameters['pwdField']} AS pwd"
		.", {$this->parameters['screenField']} AS screen"
		.", {$this->parameters['statusField']} AS status"
		." FROM {$this->parameters['sqlTable']}"
		." WHERE {$this->parameters['idField']}=".$id." LIMIT 1";
		return kapSqlUniqueResult($sql);
	}
	
	function addUser($data) {
		if (!isset($data['login']) or !isset($data['pwd']) or !isset($data['status'])
		or !isset($data['screen'])) {
			trigger_error('need keys login, pwd, status and screen',E_USER_NOTICE);
		}
		kapSqlOpen();
		$sql="INSERT INTO {$this->parameters['sqlTable']}(";
		if (!$this->parameters['autoId']) $sql.="{$this->parameters['idField']},";
		$sql.="{$this->parameters['loginField']}"
		.",{$this->parameters['pwdField']}"
		.",{$this->parameters['statusField']}"
		.",{$this->parameters['screenField']}"
		.",{$this->parameters['dateCreationField']})"
		." VALUES(";
		if (!$this->parameters['autoId']) $sql.=kapSqlSecureValue($data['id']).', ';
		$sql.=kapSqlSecureValue($data['login']).', '
		.kapSqlSecureValue($this->crypt($data['pwd'])).', '
		.kapSqlSecureValue($data['status']).', '
		.kapSqlSecureValue($data['screen']).', '
		."NOW());";
		return kapSqlExecute($sql);
	}
/*----------------------------------------------------------------------------------------------
                                                                              methodes publiques
à l'usage du noyau
----------------------------------------------------------------------------------------------*/
	// a executer à l'installation
	function install($rootPwd) {
		kapSqlOpen();
		// creation de la table
		$sql=kapFileContent(KAP_CORE_PATH.'data/kap_users.table-structure.sql');
		if (!kapSqlExecute($sql)) return false;
		// creation de l'utilisateur root
		$data=array('login'=>'root','pwd'=>$rootPwd,'screen'=>'Admin KAP','status'=>'root');
		return $this->addUser($data);
	}
/*----------------------------------------------------------------------------------------------
                                                                                méthodes privées
----------------------------------------------------------------------------------------------*/
	function crypt($pwd) {
		switch ($this->parameters['pwdEncryption']) {
			case "md5":$pwd=md5($pwd); break;
		}
		return $pwd;
	}
/*----------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------*/
}
?>
